<template>
  <div class="login">
    <div class="box-login">
      <div class="login-title"></div>
      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        label-width="0"
        style="max-width: 460px"
      >
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
            size="large"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            :type="blink ? 'text' : 'password'"
            size="large"
          >
            <template #suffix>
              <el-icon v-if="!blink" @click="blinkHandler"><Hide /></el-icon>
              <el-icon v-else @click="blinkHandler"><View /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="verifyPassword">
          <el-input
            v-model="registerForm.verifyPassword"
            :prefix-icon="Lock"
            placeholder="请确认密码"
            :type="blinkVerify ? 'text' : 'password'"
            size="large"
          >
            <template #suffix>
              <el-icon v-if="!blinkVerify" @click="blinkVerifyHandler">
                <Hide />
              </el-icon>
              <el-icon v-else @click="blinkVerifyHandler"><View /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="registerUser(registerFormRef)"
            style="width: 100%"
            type="primary"
            size="large"
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="goToLogin" type="primary" link>去登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { User, Lock, Unlock, View, Hide } from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import qs from 'qs'
import { ElMessage } from 'element-plus'
import { reguserRequest } from '../api/user.js'
const router = useRouter()

const registerFormRef = ref()
const registerForm = reactive({
  username: '',
  password: '',
  verifyPassword: '',
})
const registerRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 11, message: '用户名必须是3~11位字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码必须是大于6位的字符', trigger: 'blur' },
  ],
  verifyPassword: [
    { required: true, message: '请输入确认密码', trigger: 'blur' },
    { min: 6, message: '密码必须是大于6位的字符', trigger: 'blur' },
  ],
})
const blink = ref(false)
const blinkVerify = ref(false)

// 密码的显示与隐藏
const blinkHandler = () => {
  blink.value = !blink.value
}

// 确认密码的显示与隐藏
const blinkVerifyHandler = () => {
  blinkVerify.value = !blinkVerify.value
}

// 跳转到注册页
const goToLogin = () => {
  const params = {
    username: registerForm?.username,
    password: registerForm?.password,
  }
  router.push({
    path: '/login',
    query: params,
  })
}

// 注册请求
const registerUser = async (registerFormRef) => {
  if (!registerFormRef) return
  await registerFormRef.validate(async (valid, fields) => {
    if (valid) {
      const params = qs.stringify({
        username: registerForm.username,
        password: registerForm.password,
      })
      const { data: res } = await reguserRequest(params)
      // console.log('注册请求', res)
      const { status, message } = res
      if (status !== 0) return ElMessage.error(message)
      ElMessage.success('注册成功!')
      // 跳转到登录页
      router.push({
        name: 'login',
        query: {
          username: registerForm.username,
          password: registerForm.password,
        },
      })
    } else {
      console.log('error')
    }
  })
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;

  min-height: 100vh;
  background: url('../assets/images/login_bg.jpg') center center / cover
    no-repeat;

  .box-login {
    width: 400px;
    border-radius: 3px;
    padding: 0 20px;
    background-color: #fff;
  }

  .login-title {
    height: 60px;
    background: url('../assets/images/title.png') center center no-repeat;
  }
}
</style>
